﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20170202_TV_class
{
    class TV
    {
        private Channel currentChannel;
        private List<Channel> channelList = new List<Channel>
        {
            new Channel(1,"First"),
            new Channel(2, "Second"),
            new Channel(3, "Third"),
            new Channel(4, "Kolyan"),
            new Channel(5, "Five")
        };
   

        public void SwitchChannelUp()
        {
            if ((currentChannel == null) || (currentChannel == channelList.Last()))
            {
                currentChannel = channelList[0];
            }
            else
            {
                currentChannel = channelList[(channelList.IndexOf(currentChannel) + 1)];
            }
        }

        public void SwitchChannelDown()
        {
            if (currentChannel == null)
            {
                currentChannel = channelList[0];
            }
            else if (currentChannel == channelList[0])
            {
                currentChannel = channelList.Last();
            }
            else
            {
                currentChannel = channelList[(channelList.IndexOf(currentChannel) - 1)];
            }
        }

        public void ShowCurrentChannel()
        {
            currentChannel.PrintChannelInfo();
        }

        public void SwitchInputSelector()
        {
            ConsoleKeyInfo cki;
            Console.WriteLine("Press arrows <Up> and <Down> to switch channels.\nPress <Space Bar> to show current channel.\nAny other key stops the program.");
            cki = Console.ReadKey();
            while(cki.Key == ConsoleKey.UpArrow || cki.Key == ConsoleKey.DownArrow || cki.Key == ConsoleKey.Spacebar)
            {
                cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.UpArrow) SwitchChannelUp();
                if (cki.Key == ConsoleKey.DownArrow) SwitchChannelDown();
                if (cki.Key == ConsoleKey.Spacebar) ShowCurrentChannel();
            }


            //do
            //{
            //    cki = Console.ReadKey();
            //    if (cki.Key == ConsoleKey.UpArrow) SwitchChannelUp();
            //    if (cki.Key == ConsoleKey.DownArrow) SwitchChannelDown();
            //    if (cki.Key == ConsoleKey.Spacebar) ShowCurrentChannel();
            //    while (cki.Key == ConsoleKey.UpArrow || cki.Key == ConsoleKey.DownArrow || cki.Key == ConsoleKey.DownArrow) ;
            //}
        }
    }
}
